<?php
require_once(ABSPATH.'/DB.php');
class Post	{

//	}
	/**
	 * get the post detail bye input $id
	 *
	 * @param unknown_type $id
	 */
	function get_post_valid () {
		#$this -> init();
		$db = new DB();
		$db -> conn_mysql();
		if ($_GET[id] != ''){				
			$sql = "select * from post where id  = $_GET[id] and valid =1 ";
			
			$get_post_res =   $db -> run_sql_query($sql);
			#$post_info = mysql_fetch_row($get_post_res);
			$post_info = mysql_fetch_array($get_post_res);
			return $post_info;
		}
		
	}
	
	function get_post () {

		$db = new DB();
		$db -> conn_mysql();
		if (!empty($_GET[id])){
			$sql = "select *  from post p ,catalog c where p.id  = $_GET[id] and p.catalog_id = c.id ";			
			$get_post_res =   $db -> run_sql_query($sql);
			$post_info = mysql_fetch_array($get_post_res);			
			return $post_info;
		}
	}

	function get_post_all_valid() {
		$db = new DB();
		$db -> conn_mysql();
		$sql = "select * from post where valid = 0";
		$get_posts_rec = $db -> run_sql_query($sql);
		while ($post_info =  mysql_fetch_array($get_posts_rec)) {
			$posts_list[] = $post_info;
		}
		
		return $posts_list;
	}
	
	function get_posts_all_dirty() {
		$db = new DB();
		$db -> conn_mysql();
		$sql = "select * from post where valid = 0";
		$get_posts_rec = $db -> run_sql_query($sql);
		while ($post_info =  mysql_fetch_array($get_posts_rec)) {
			$posts_list[] = $post_info;
		}
		
		return $posts_list;
	}
	
	/**
	 * get all posts from the database include the posts had been deleted
	 *
	 * @return unknown
	 */
	function get_posts_all() {
		$db = new DB();
		$db -> conn_mysql();
		$sql = "select * from post";
		$get_posts_rec = $db -> run_sql_query($sql);
		while ($post_info =  mysql_fetch_array($get_posts_rec)) {
			$posts_list[] = $post_info;
		}
//		echo count($posts_list);
		return $posts_list;
	}
	
	
	function add_new_post()  {
		$db = new DB();
		$db -> conn_mysql();
		$sql = "insert into post values ('',now(),now(),'$_POST[title]','$_POST[content]','$_SESSION[USER_NAME]','1','$_POST[catalog]')";
		echo $sql;
		$result = $db -> run_sql_query($sql);		
		if ($result) {
			return  true;
		}
	}
	
	function update_post () {
		$db = new DB();
		$db -> conn_mysql();
		$sql = "update post set date_modified = now(),title='$_POST[title]',content='$_POST[content]',catalog_id='$_POST[catalog]' where id ='$_GET[id]'";
		$result = $db -> run_sql_query($sql);		
		if ($result) {
			return  true;
		}
	}
	
	function delete_post() {
		if (!empty($_GET[id])) {
			$db = new DB();
			$db -> conn_mysql();
			$sql = "update post set valid =0 ,date_modified = now() where id = $_GET[id]";
			echo  $sql;
			$result = $db -> run_sql_query($sql);
		}
	}
	
	function rows_num() {
		$db = new DB();
		$db -> conn_mysql();
		$sql = "select * from post where valid = 0";
		$result  = $db -> run_sql_query($sql);
		$nums_rows  = mysql_num_rows($result);
		return $nums_rows;
	}
}

?>